﻿/*
* BaseHero.cpp
*
* 武将基类
*
* create date: 2014.4.11
* create by: leo
*
*/

#include "BaseHero.h"

#include "../skill/SkillBuilder.h"
#include "../skill/Walking.h"
#include "../skill/Waiting.h"
#include "../skill/BeAttacked.h"
#include "../skill/Dying.h"
#include "../skill/Victory.h"
#include "../skill/CommonAttack.h"
#include "../skill/throw/BaseThrowObj.h"
#include "../corps/BaseCorps.h"

USING_NS_CC;
USING_NS_CC_EXT;

//
CBaseHero* CBaseHero::create(const char* paramString)
{
	// parse param..
	CCString ArmaResPath;
	CCString ArmaName;
	if (NULL != strstr(paramString, "guanyu"))
	{
		ArmaResPath = "guanyu2/guanyu2-1.ExportJson";
		ArmaName = "guanyu2-1";
	}
	else if (NULL != strstr(paramString, "zhugeliang"))
	{
		ArmaResPath = "zhugeliang/zhugeliang.ExportJson";
		ArmaName = "zhugeliang";
	}

	CHeroArmature* armature = NULL;

	CBaseHero::Param* param = new CBaseHero::Param(
			1, // id
			*(CCString::create("")), // name
			1, // level,
			0, // exp,
			1, // race,
			0, // rank,
			0, // type,
			1, // starRank,
			100, // Hp,
			1, // str,
			1, // intell,
			1, // crit,
			1, // revert,
			1, // armor,
			1, // magicResist,
			1, // critResist,
			1, // rage,
			1, // hpGrowth,
			1, // strGrowth,
			1, // intellGrowth,
			2, // attSpeed,
            150, // moveSpeed,
			armature,
			0.4f
			);

	(*param).ArmaResPath = ArmaResPath;
	(*param).ArmaName = ArmaName;

	CBaseSkill::Param* aParam = new CBaseSkill::Param();
	(*aParam).SkillName = "waiting";
	(*param).SkillParams.push_back(aParam);

	aParam = new CBaseSkill::Param();
	(*aParam).SkillName = "walking";
	(*param).SkillParams.push_back(aParam);

	aParam = new CBaseSkill::Param();
	(*aParam).SkillName = "beAttacked";
	(*param).SkillParams.push_back(aParam);

	aParam = new CBaseSkill::Param();
	(*aParam).SkillName = "dying";
	(*param).SkillParams.push_back(aParam);

	aParam = new CBaseSkill::Param();
	(*aParam).SkillName = "victory";
	(*param).SkillParams.push_back(aParam);

	aParam = new CBaseSkill::Param();
	(*aParam).SkillName = "commonAttack";
	(*aParam).Type = CBaseSkill::COMMON_ATTACK;
	(*param).SkillParams.push_back(aParam);

	aParam = new CBaseSkill::Param();
	(*aParam).SkillName = "autoSkill1";
	(*aParam).Type = CBaseSkill::AUTO_SKILL_1;
	(*param).SkillParams.push_back(aParam);


	if (NULL != strstr(paramString, "guanyu"))
	{
		(*param).SkillIndices.push_back(0);
		(*param).SkillIndices.push_back(1);
		(*param).SkillIndices.push_back(6);
		(*param).SkillIndices.push_back(4);
		(*param).SkillIndices.push_back(5);
		(*param).SkillIndices.push_back(2);
		(*param).SkillIndices.push_back(3);

		(*param).Scale = 0.4f;
		(*param).SkillParams[CBaseSkill::COMMON_ATTACK]->AttackRange = 160;
		(*param).SkillParams[CBaseSkill::COMMON_ATTACK]->EffectValue = -10;

		(*param).SkillParams[CBaseSkill::AUTO_SKILL_1]->AttackRange = 160;
		(*param).SkillParams[CBaseSkill::AUTO_SKILL_1]->EffectValue = -30;

		(*param).SkillSequences.push_back(CBaseSkill::COMMON_ATTACK);
		(*param).SkillSequences.push_back(CBaseSkill::COMMON_ATTACK);
		(*param).SkillSequences.push_back(CBaseSkill::AUTO_SKILL_1);
	}
	else if (NULL != strstr(paramString, "zhugeliang"))
	{
		(*param).SkillIndices.push_back(0);
		(*param).SkillIndices.push_back(4);
		(*param).SkillIndices.push_back(1);
		(*param).SkillIndices.push_back(2);
		(*param).SkillIndices.push_back(6);
		(*param).SkillIndices.push_back(5);
		(*param).SkillIndices.push_back(3);

		(*param).Scale = 0.25f;
		(*param).SkillParams[CBaseSkill::COMMON_ATTACK]->AttackRange = 660;
		(*param).SkillParams[CBaseSkill::COMMON_ATTACK]->EffectValue = -20;

		(*param).SkillParams[CBaseSkill::AUTO_SKILL_1]->AttackRange = 660;
		(*param).SkillParams[CBaseSkill::AUTO_SKILL_1]->EffectValue = -40;

		CCAnimation* animation = CCAnimation::create();
		animation->addSpriteFrameWithFileName("throwable/blue_ball/blue_ball_1.png");
		animation->addSpriteFrameWithFileName("throwable/blue_ball/blue_ball_2.png");
		animation->addSpriteFrameWithFileName("throwable/blue_ball/blue_ball_3.png");
		animation->addSpriteFrameWithFileName("throwable/blue_ball/blue_ball_4.png");
		animation->setDelayPerUnit(0.1f);
		animation->setLoops(1);

        CBaseThrowObj* tObj = CBaseThrowObj::create(NULL, NULL, NULL, CRouteGeneratorManager::LINE, animation, 0.5, 0);
		tObj->setSpeed(600);
		(*param).SkillParams[CBaseSkill::COMMON_ATTACK]->ThrowObjs->addObject(tObj);

		CCAnimation* animation1 = CCAnimation::create();
		for (int i = 3; i < 23; i++)
		{
			animation1->addSpriteFrameWithFileName(CCString::createWithFormat("throwable/aerolite/w%d.png", i)->getCString());
		}
		animation1->setDelayPerUnit(0.1f);
		animation1->setLoops(1);

        CBaseThrowObj* tObj1 = CBaseThrowObj::create(NULL, NULL, NULL, CRouteGeneratorManager::FIX, animation1, 1, 2);
		tObj1->setSpeed(0);
		(*param).SkillParams[CBaseSkill::AUTO_SKILL_1]->ThrowObjs->addObject(tObj1);

		(*param).SkillSequences.push_back(CBaseSkill::COMMON_ATTACK);
		(*param).SkillSequences.push_back(CBaseSkill::COMMON_ATTACK);
		(*param).SkillSequences.push_back(CBaseSkill::AUTO_SKILL_1);
	}


	CBaseHero* hero = new CBaseHero(*param);

	return hero;
}

void CBaseHero::changeStateTo(int state)
{}

void CBaseHero::walkingTo(const cocos2d::CCPoint& pos/*, cocos2d::CCObject* callbackObj, cocos2d::SEL_CallFuncO callback*/)
{
	CWalking& walking = *(CWalking*)mSkills[CBaseSkill::WALKING];
	walking.setDestination(pos.x, pos.y);
	walking.setOnSkillFinishCallback(this, callfuncO_selector(CBaseHero::onSkillFinish));
	walking.use();
	mMoving = true;
}

void CBaseHero::stopWalking()
{
	CWalking& walking = *(CWalking*)mSkills[CBaseSkill::WALKING];
	walking.stop();
	mMoving = false;
}

void CBaseHero::registerWalkingSkill(CWalking& walking)
{
	while (mSkills.size() <= CBaseSkill::WALKING)
	{
		CBaseSkill::Param param;
		CBaseSkill* skill = CSkillBuilder::getInstance().createSkill(CBaseSkill::EMPTY_SKILL, param);
		mSkills.push_back(skill);
	}

	if (&walking != mSkills[CBaseSkill::WALKING])
	{
		delete mSkills[CBaseSkill::WALKING];
		mSkills[CBaseSkill::WALKING] = &walking;
	}
}

void CBaseHero::waiting()
{
	CWaiting& waiting = *(CWaiting*)mSkills[CBaseSkill::WAITING];
	waiting.setOnSkillFinishCallback(this, callfuncO_selector(CBaseHero::onSkillFinish));
	waiting.use();
	mSkillActing = false;
}

void CBaseHero::registerWaitingSkill(CWaiting& waiting)
{
	while (mSkills.size() <= CBaseSkill::WAITING)
	{
		CBaseSkill::Param param;
		CBaseSkill* skill = CSkillBuilder::getInstance().createSkill(CBaseSkill::EMPTY_SKILL, param);
		mSkills.push_back(skill);
	}

	CBaseSkill* skill = mSkills[CBaseSkill::WAITING];
	if (&waiting != skill)
	{
		mSkills[CBaseSkill::WAITING] = &waiting;
		delete skill;
	}
}

void CBaseHero::beAttacked(CBaseSkill& skill)
{
	stopWalking();
	mElapsedTime = 0;

	int effectValue = skill.getSkillEffectValue();
//	CCLabelTTF* effectValueLabel = CCLabelTTF::create("", "", 20);
	CCLabelAtlas* effectValueLabel = CCLabelAtlas::create("", "red.png", 35, 48, '0');
    effectValueLabel->setAnchorPoint(ccp(0.5, 0.5));

	effectValueLabel->setPosition(this->getPosition());
	effectValueLabel->setPositionY(effectValueLabel->getPositionY() + this->getContentSize().height * this->mParam.Scale);
	effectValueLabel->setScale(0.1);
	effectValueLabel->runAction(CCSequence::create(CCEaseElasticOut::create(CCScaleTo::create(0.2, skill.isCrit() ? 0.8 : 0.5)),
			CCSpawn::createWithTwoActions(CCMoveBy::create(1, ccp(0, 50)), CCFadeOut::create(1)),
			CCCallFuncN::create(this, callfuncN_selector(CBaseHero::removeNode)),
			NULL));

	CCDirector::sharedDirector()->getRunningScene()->addChild(effectValueLabel, 99999);

	mHpBarBg->setOpacity(255);
	mHpBar->setOpacity(255);
	mHpBarBg->runAction(CCSequence::create(CCShow::create(), CCDelayTime::create(2), CCFadeOut::create(1), CCHide::create(), NULL));
	mHpBar->runAction(CCSequence::create(CCShow::create(), CCDelayTime::create(2), CCFadeOut::create(1), CCHide::create(), NULL));

	if (effectValue < 0)
	{
		effectValueLabel->setString(CCString::createWithFormat(":%d", abs(effectValue))->getCString());
		effectValueLabel->setColor(ccRED);
		mParam.Hp += effectValue;
		mHpBar->setScaleX(mParam.Hp * 1.0 / mParam.MaxHp);
		if (mParam.Hp <= 0)
		{
			dying();
		}
		else
		{
			CBeAttacked& beAttacked = *(CBeAttacked*)mSkills[CBaseSkill::BEATTACKED];
			beAttacked.setOnSkillFinishCallback(this, callfuncO_selector(CBaseHero::onSkillFinish));
			beAttacked.use();
		}
	}
	else if (effectValue > 0)
	{

	}

}

void CBaseHero::registerBeAttackedSkill(CBeAttacked& beAttacked)
{
	while (mSkills.size() <= CBaseSkill::BEATTACKED)
	{
		CBaseSkill::Param param;
		CBaseSkill* skill = CSkillBuilder::getInstance().createSkill(CBaseSkill::EMPTY_SKILL, param);
		mSkills.push_back(skill);
	}

	if (&beAttacked != mSkills[CBaseSkill::BEATTACKED])
	{
		delete mSkills[CBaseSkill::BEATTACKED];
		mSkills[CBaseSkill::BEATTACKED] = &beAttacked;
	}
}

void CBaseHero::dying()
{
	CDying& dying = *(CDying*)mSkills[CBaseSkill::DYING];
	dying.setOnSkillFinishCallback(this, callfuncO_selector(CBaseHero::onDying));
	dying.use();

	mMoving = false;
}

void CBaseHero::registerDyingSkill(CDying& dying)
{
    while (mSkills.size() <= CBaseSkill::DYING)
	{
		CBaseSkill::Param param;
		CBaseSkill* skill = CSkillBuilder::getInstance().createSkill(CBaseSkill::EMPTY_SKILL, param);
		mSkills.push_back(skill);
	}

    if (&dying != mSkills[CBaseSkill::DYING])
	{
		delete mSkills[CBaseSkill::DYING];
		mSkills[CBaseSkill::DYING] = &dying;
	}
}

void CBaseHero::victory()
{
	unscheduleUpdate();

	CVictory& victory = *(CVictory*)mSkills[CBaseSkill::VICTORY];
	victory.use();

	mMoving = false;
}

void CBaseHero::registerVictorySkill(CVictory& victory)
{
    while (mSkills.size() <= CBaseSkill::VICTORY)
	{
		CBaseSkill::Param param;
		CBaseSkill* skill = CSkillBuilder::getInstance().createSkill(CBaseSkill::EMPTY_SKILL, param);
		mSkills.push_back(skill);
	}

    if (&victory != mSkills[CBaseSkill::VICTORY])
	{
		delete mSkills[CBaseSkill::DYING];
		mSkills[CBaseSkill::VICTORY] = &victory;
	}
}

void CBaseHero::registerCommonAttackSkill(CCommonAttack& attack)
{
	while (mSkills.size() <= CBaseSkill::COMMON_ATTACK)
	{
		CBaseSkill::Param param;
		CBaseSkill* skill = CSkillBuilder::getInstance().createSkill(CBaseSkill::EMPTY_SKILL, param);
		mSkills.push_back(skill);
	}

	if (&attack != mSkills[CBaseSkill::COMMON_ATTACK])
	{
		delete mSkills[CBaseSkill::COMMON_ATTACK];
		mSkills[CBaseSkill::COMMON_ATTACK] = &attack;
	}

}

void CBaseHero::registerSkill(CBaseSkill& skill, CBaseSkill::Type type)
{
	while (mSkills.size() <= type)
	{
		CBaseSkill::Param param;
		CBaseSkill* skill = CSkillBuilder::getInstance().createSkill(CBaseSkill::EMPTY_SKILL, param);
		mSkills.push_back(skill);
	}

	if (&skill != mSkills[type])
	{
		delete mSkills[type];
		mSkills[type] = &skill;
	}
}

void CBaseHero::preloadRes()
{
	CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo(mParam.ArmaResPath.getCString());

	if (NULL == mParam.Armature)
	{
		mParam.Armature = CHeroArmature::create(mParam.ArmaName.getCString());
		mParam.ShadowArmature = CHeroArmature::create(mParam.ArmaName.getCString());
	}

	mParam.Armature->setScale(mParam.Scale);
	mParam.ShadowArmature->setScale(mParam.Scale);

	mParam.Armature->setPosition(CCPointZero);
	mParam.ShadowArmature->setPosition(CCPointZero);
	mParam.ShadowArmature->setScaleY(-0.2 * mParam.Scale);
//	mParam.ShadowArmature->setColor(ccGRAY);
//	mParam.ShadowArmature->setSkewX(30);

	CCGLProgram* p = new CCGLProgram();
	p->initWithVertexShaderFilename("gray.vs", "gray.fs");

	p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
	p->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
	p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
	p->link();
	p->updateUniforms();

//	mParam.ShadowArmature->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
	mParam.ShadowArmature->setShaderProgram(p);

	if (NULL == mParam.Armature->getParent())
	{
		this->addChild(mParam.ShadowArmature);
		this->addChild(mParam.Armature);

		mHpBarBg->setOpacity(0);
		mHpBar->setOpacity(0);

		mHpBarBg->setPositionX(-this->getContentSize().width * mParam.Scale * 0.5);
		mHpBar->setPositionX(-this->getContentSize().width * mParam.Scale * 0.5);

		mHpBarBg->setPositionY(mHpBarBg->getPositionY() + this->getContentSize().height * this->mParam.Scale + 20);
		mHpBar->setPositionY(mHpBar->getPositionY() + this->getContentSize().height * this->mParam.Scale + 20);
	}


	int i = 0;
	for (std::vector<CBaseSkill*>::iterator it = mSkills.begin(); it != mSkills.end(); it++)
	{
		CBaseSkill* skill = (CBaseSkill*)(*it);
		skill->setArmature(*(mParam.Armature), mSkillArmatureIndices[i]);
		skill->setShadowArmature(*(mParam.ShadowArmature), mSkillArmatureIndices[i++]);
	}

	mResLoaded = true;
}

void CBaseHero::unloadRes()
{
	mResLoaded = false;
}

bool CBaseHero::isResLoaded()
{
	return mResLoaded;
}

void CBaseHero::setCorps(CBaseCorps& mCorps)
{
	CC_SAFE_RELEASE(mBelongCorps);
	mBelongCorps = &mCorps;
	mBelongCorps->retain();
}

void CBaseHero::setEnemyCorps(CBaseCorps& mCorps)
{
	CC_SAFE_RELEASE(mEnemyCorps);
	mEnemyCorps = &mCorps;
	mEnemyCorps->retain();
}

CBaseCorps* CBaseHero::getCorps()
{
	return mBelongCorps;
}

CBaseCorps* CBaseHero::getEnemyCorps()
{
	return mEnemyCorps;
}

int CBaseHero::getMoveSpeed()
{
	return mParam.MoveSpeed;
}

bool CBaseHero::isAlive()
{
	return mParam.Hp > 0;
}

float CBaseHero::getArmatureScale()
{
	return mParam.Scale;
}

void CBaseHero::setCorpsPos(CBaseCorps::CorpsPos pos)
{
	mCorpsPos = pos;
}

CBaseCorps::CorpsPos CBaseHero::getCorpsPos()
{
	return mCorpsPos;
}

void CBaseHero::addStateListener(IBaseHeroStateListener& listener)
{
	if (NULL != &listener && mStateListeners.end() == find(mStateListeners.begin(), mStateListeners.end(), &listener))
	{
		mNeedAddStateListeners.push_back(&listener);
	}
}

void CBaseHero::removeStateListener(IBaseHeroStateListener& listener)
{
	mNeedRemoveStateListeners.push_back(&listener);
}

CCPoint CBaseHero::getAttackPoint()
{
	CCBone* attackBone = mParam.Armature->getBone("Layer53");
    if (NULL != attackBone)
    {
	    return ccp(this->getPositionX() + attackBone->getWorldInfo()->x * this->getArmatureScale(),
			    this->getPositionY() + attackBone->getWorldInfo()->y * this->getArmatureScale());
    }
    else
    {
        return this->getPosition();
    }
}

int CBaseHero::getCrit()
{
	return mParam.Crit;
}

void CBaseHero::turnTo(Direction dir)
{
    if (dir != mDir)
    {
        mSkewAniValue = -mSkewAniValue;
        mSkewAniValueStep = -mSkewAniValueStep;
    }

    mDir = dir;

	switch (dir)
	{
	case LEFT:
		this->mParam.Armature->setScaleX(-fabs(this->mParam.Armature->getScaleX()));
		this->mParam.ShadowArmature->setScaleX(-fabs(this->mParam.ShadowArmature->getScaleX()));
		
		break;

	case RIGHT:
		this->mParam.Armature->setScaleX(fabs(this->mParam.Armature->getScaleX()));
		this->mParam.ShadowArmature->setScaleX(fabs(this->mParam.ShadowArmature->getScaleX()));

		break;

	default:
		break;
	}
}

void CBaseHero::update(float delta)
{
	mSkewAniValue += mSkewAniValueStep * delta;
	mParam.ShadowArmature->setSkewX(mSkewAniValue);

	for (std::list<IBaseHeroStateListener*>::iterator it = mNeedAddStateListeners.begin(); it != mNeedAddStateListeners.end(); it++)
	{
		IBaseHeroStateListener* listener = (IBaseHeroStateListener*)(*it);
		mStateListeners.push_back(listener);
	}
    mNeedAddStateListeners.clear();

	for (std::list<IBaseHeroStateListener*>::iterator it = mNeedRemoveStateListeners.begin(); it != mNeedRemoveStateListeners.end(); it++)
	{
		IBaseHeroStateListener* listener = (IBaseHeroStateListener*)(*it);
		mStateListeners.remove(listener);
	}
    mNeedRemoveStateListeners.clear();

	if (mMoving)
	{
		for (std::list<IBaseHeroStateListener*>::iterator it = mStateListeners.begin(); it != mStateListeners.end(); it++)
		{
			IBaseHeroStateListener* listener = (IBaseHeroStateListener*)(*it);
			listener->onPositionChange(*this);
		}
	}

	if (mSkillActing) return;

	if (100 == mParam.Rage)
	{

		mParam.Rage = 0;
		mElapsedTime = 0;
		return;
	}

	mElapsedTime += delta;
	if (mElapsedTime >= mParam.AttSpeed)
	{
		if (mSkillSequence.count() > 0)
		{
			CBaseSkill* nextSkill = (CBaseSkill*)mSkillSequence.objectAtIndex(mNextSkillIndex);
			nextSkill->setOnSkillFinishCallback(this, callfuncO_selector(CBaseHero::onSkillFinish));
			mSkillActing = true;
			nextSkill->use();
			mNextSkillIndex++;
			if (mNextSkillIndex >= mSkillSequence.count())
			{
				mNextSkillIndex = 0;
			}
		}

		mElapsedTime = 0;
	}
}

const CCSize& CBaseHero::getContentSize() const
{
	return mParam.Armature->getContentSize();
}

void CBaseHero::setPosition(const CCPoint &position)
{
	CCNode::setPosition(position);
	for (std::list<IBaseHeroStateListener*>::iterator it = mStateListeners.begin(); it != mStateListeners.end(); it++)
	{
		IBaseHeroStateListener* listener = (IBaseHeroStateListener*)(*it);
		listener->onPositionChange(*this);
	}
}

CBaseHero::CBaseHero()
: mFilePathProv(CFilePathProvider::getInstance()),
  mResLoaded(false),
  mSkillActing(false),
  mMoving(false),
  mCorpsPos(CBaseCorps::FIRST),
  mBelongCorps(NULL),
  mEnemyCorps(NULL),
  mDir(RIGHT),
  mElapsedTime(0),
  mNextSkillIndex(0)
{}

CBaseHero::CBaseHero(Param& param)
: mFilePathProv(CFilePathProvider::getInstance()),
  mResLoaded(false),
  mSkillActing(false),
  mMoving(false),
  mCorpsPos(CBaseCorps::FIRST),
  mBelongCorps(NULL),
  mEnemyCorps(NULL),
  mDir(RIGHT),
  mParam(param),
  mElapsedTime(0),
  mNextSkillIndex(0)
{
//	CCString waitingSkill("waiting");
//	CCString walkingSkill("walking");
//	CCString beAttackedSkill("beAttacked");
//	CCString attackSkill("attack");

//	CBaseSkill* waiting = CSkillBuilder::getInstance().createSkill(CBaseSkill::WAITING, this, waitingSkill);
//	CBaseSkill* walking = CSkillBuilder::getInstance().createSkill(CBaseSkill::WALKING, this, walkingSkill);
//	CBaseSkill* beAttacked = CSkillBuilder::getInstance().createSkill(CBaseSkill::BEATTACKED, this, beAttackedSkill);
//	CBaseSkill* attack = CSkillBuilder::getInstance().createSkill(CBaseSkill::COMMON_ATTACK, this, attackSkill);

//	waiting->release();
//	walking->release();
//	beAttacked->release();
//	attack->release();

	int skillType = CBaseSkill::WAITING;

	for (std::vector<CBaseSkill::Param*>::iterator it = param.SkillParams.begin(); it != param.SkillParams.end(); it++)
	{
		CBaseSkill::Param* aParam = *it;
		aParam->BelongHero = this;
		CBaseSkill* skill = CSkillBuilder::getInstance().createSkill(skillType++, *aParam);
	}

//	hero->mSkills.addObject(waiting);
//	hero->mSkills.addObject(walking);


	for (std::vector<int>::iterator it = param.SkillSequences.begin(); it != param.SkillSequences.end(); it++)
	{
		mSkillSequence.addObject(mSkills[*it]);
	}

	mSkillArmatureIndices.insert(mSkillArmatureIndices.begin(), param.SkillIndices.begin(), param.SkillIndices.end());

	mHpBarBg = CCLayerColor::create(ccc4(200, 200, 200, 255), 100, 14);
	mHpBar = CCLayerColor::create(ccc4(255, 100, 100, 255), 100, 14);

//	mHpBarBg->setVisible(false);
//	mHpBar->setVisible(false);
	mHpBarBg->setAnchorPoint(ccp(0, 0));
	mHpBar->setAnchorPoint(ccp(0, 0));

	this->addChild(mHpBarBg, 1000);
	this->addChild(mHpBar, 1000);

	mSkewAniValue = 30;
	mSkewAniValueStep = -2;
}

CBaseHero::~CBaseHero()
{
    /*
	std::vector<CBaseSkill::Param*>::iterator it = mParam.SkillParams.begin();
	for (; it != mParam.SkillParams.end(); it++)
	{
		delete (*it);
	}
    */
	mParam.SkillParams.clear();

	CC_SAFE_RELEASE(mBelongCorps);
	CC_SAFE_RELEASE(mEnemyCorps);
}

void CBaseHero::onSkillFinish(cocos2d::CCObject* skill)
{
	mSkillActing = false;
	mMoving = false;
}

void CBaseHero::onDying(cocos2d::CCObject* skill)
{
	unscheduleUpdate();
	mParam.Armature->runAction(CCSequence::create(CCDelayTime::create(1), CCFadeOut::create(2), NULL));
	mParam.ShadowArmature->runAction(CCSequence::create(CCDelayTime::create(1), CCFadeOut::create(2), NULL));

	bool needNotify = true;
	for (std::list<IBaseHeroStateListener*>::iterator it = mStateListeners.begin(); it != mStateListeners.end(); it++)
	{
		needNotify = true;
		IBaseHeroStateListener* listener = (IBaseHeroStateListener*)(*it);
		for (std::list<IBaseHeroStateListener*>::iterator itt = mNeedRemoveStateListeners.begin(); itt != mNeedRemoveStateListeners.end(); itt++)
		{
			if ((*itt) == (*it))
			{
				needNotify = false;
			}
		}

		if (needNotify)
		{
			listener->onDied(*this);
		}
	}
}

void CBaseHero::removeNode(cocos2d::CCNode* node)
{
	node->removeFromParent();
}
